<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Validator
{
	private $_string = null;
	
	/**
	 * @return Gio_Core_Validator
	 */
	public function __construct($string = null)
	{
		$this->_string = $string;
		return $this;
	}

	/**
	 * @return Gio_Core_Validator
	 */
	public function setString($string)
	{
		$this->_string = $string;
		return $this;
	}
	
	public function isUsername()
	{
		if (!preg_match("!^[a-z]{1,}[a-z0-9_]{4,20}$!", $this->_string)) {
        	return false;
		} 
		return true;
	}
	
	public function isIdString()
	{
		if (!preg_match("!^[a-z0-9]{2,50}$!i", $this->_string)) {
        	return false;
		} 
		return true;
	}
	
	public function isSlug()
	{
		if (!preg_match("!^[a-z0-9_-]{2,255}$!i", $this->_string)) {
        	return false;
		} 
		return true;
	}
	
	public function isCharacter()
	{
		if (!preg_match("!^[a-zA-Z0-9_~]{2,20}$!i", $this->_string)) {
        	return false;
		} 
		return true;
	}
	
	public function notNull()
	{
		return (null == trim($this->_string)) ? false : true;
	}
	
	public function isEmail()
	{
		if (!preg_match("/^([a-zA-Z0-9_.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $this->_string)) {
			return false;
		}
		return true;
	}
	
	public function isNumeric()
	{
		if ((int)$this->_string == 0) {
			return false;
		}
		return is_numeric($this->_string);
	}
	
	public function minlength($min = 10)
	{
		return true;	
	}
	
	public function maxlength($max = 255)
	{
		return true;
	}
	
	public function compare($string)
	{
		return ($this->_string == $string);	
	}
	
	public function validate($arrayFunction = array(), $arrayMessages = array())
	{
		if (empty($arrayFunction)) {
			return;
		}
		foreach ($arrayFunction as $index => $function) {
			if (gettype($function) == 'array') {
				if (!call_user_func_array(array($this, $index), $function)) {
					return $arrayMessages[$index];
				}
			} else {
				if (!$this->{$function}() && isset($arrayMessages[$function])) {
					return $arrayMessages[$function];
				}
			}
		}
		return null;
	}
}